﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using Office.Drawing;

namespace Office.Chart.UI.Internals
{
    class PieSeriesViewModel : ShapedViewModel
    {
        private readonly PieSeries _pieSeries;

        public PieSeriesViewModel(PieSeries pieSeries) : base(pieSeries)
        {
            _pieSeries = pieSeries;
            WeakEventHelper.AddWeakPropertyChanged(_pieSeries, onPieSeriesChanged);
            if (_pieSeries.Values != null)
                WeakEventHelper.AddWeakValueChanged(_pieSeries, onPieSeriesChanged2);
        }

        private void onPieSeriesChanged2(object sender, EventArgs args)
        {
            NotifyPropertyChanged("Values");
        }

        private void onPieSeriesChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Categories":
                    NotifyPropertyChanged("Values");
                    break;
                case "Values":
                    {
                        NotifyPropertyChanged("Values");
                    }
                    break;
                default:
                    Debug.WriteLine("not handled " + e.PropertyName);
                    break;
            }
        }

        void _pieSeries_ValueChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged("Values");
        }

        public IEnumerable<CategoryValue> Values
        {
            get
            {
                if (_pieSeries.Categories == null || true)
                {
                    int index = 0;
                    foreach (var v in _pieSeries.Values.NumericValues)
                        yield return new CategoryValue(v.Value, v.Value, index++);
                    yield break;
                }
                throw new NotSupportedException();
               // var categories = this._pieSeries.Categories;

            }
        }

    }

    internal class CategoryValue
    {
        private readonly object _category;
        private readonly double _value;
        private int _index;

        public CategoryValue(object category, double value, int index)
        {
            Contract.Requires(category != null);
            _index = index;
            _category = category;
            _value = value;
        }
        public object Text { get { return this; } } //\\hack
        public string ActualValue { get { return Value.ToString(); } }
        public ShapeProperties ShapeProperties { get { return null; } }
        public int Index { get { return _index; } }

        public object Category
        {
            get { return _category; }
        }

        public double Value
        {
            get { return _value; }
        }
    }
}
